import { Component, OnInit, Inject, Type } from '@angular/core';
import { SelectItem } from 'primeng/primeng';
import { IContextBroker, ICONTEXT_BROKER_TOKEN, IContext } from '../../../src/index';
import { getAllContextTypes, getContextDesc, IdentityContext, getContextDetail } from '../utils/sample.context';

@Component({
  selector: 'app-context-center',
  templateUrl: './context-center.component.html',
  styleUrls: ['./context-center.component.scss']
})
export class ContextCenterComponent implements OnInit {

  contextTypes: SelectItem[] = [];
  contextDescs: SelectItem[] = [];
  contextDetails: SelectItem[] = [];

  constructor(
    @Inject(ICONTEXT_BROKER_TOKEN) private _broker: IContextBroker) {


  }

  ngOnInit() {
    let self = this;
    let allTypes = getAllContextTypes();
    this.contextTypes = allTypes;
    allTypes.map(item => item.value.type as string)
      .forEach(ct => {
        self._broker.registerListener(ct, {
          next: (c: IdentityContext) => {
            console.log(c);
            self.contextDescs = [{
              label: getContextDesc(c),
              value: c.contextClass,
            }].concat(self.contextDescs);

            self.contextDetails = [{
              label: getContextDetail(c),
              value: c.contextClass,
            }].concat(self.contextDetails);
          },
          error: (error: any) => {
            console.log(error);
          },
          complete: () => {
          }
        })
      })
  }

}
